package com.supermarket.controller;

import com.supermarket.annotation.OperationLogAnnotation;
import com.supermarket.dto.StockPageQueryDto;
import com.supermarket.entity.Stock;
import com.supermarket.service.StockService;
import com.supermarket.utils.PageResult;
import com.supermarket.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/stock")
@Slf4j
@Api(tags = "库存管理", description = "提供库存相关的操作接口")  // 类级别的 Swagger 注解
public class StockController {

    @Autowired
    private StockService stockService;

    /**
     * 分页查询库存列表
     */
    @ApiOperation(value = "分页查询库存列表", notes = "根据分页条件查询库存列表")
    @GetMapping("/list")
    public Result<PageResult> list(StockPageQueryDto stockPageQueryDto) {
        log.info("分页查询库存列表：{}", stockPageQueryDto);
        PageResult pageResult = stockService.list(stockPageQueryDto);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询库存
     */
    @ApiOperation(value = "根据id查询库存", notes = "根据库存id获取库存信息")
    @GetMapping("/{id}")
    public Result<Stock> getById(@ApiParam(value = "库存ID", required = true) @PathVariable Long id) {
        log.info("根据id查询库存：{}", id);
        Stock stock = stockService.getById(id);
        return Result.success(stock);
    }

    /**
     * 根据商品id查询库存
     */
    @ApiOperation(value = "根据商品id查询库存", notes = "根据商品ID获取库存信息")
    @GetMapping("/product/{productId}")
    public Result<Stock> getByProductId(@ApiParam(value = "商品ID", required = true) @PathVariable Long productId) {
        log.info("根据商品id查询库存：{}", productId);
        Stock stock = stockService.getByProductId(productId);
        return Result.success(stock);
    }

    /**
     * 新增库存
     */
    @ApiOperation(value = "新增库存", notes = "新增库存信息")
    @PostMapping
    @OperationLogAnnotation(operation = "新增库存", operationType = "新增")
    public Result<String> save(@ApiParam(value = "库存信息", required = true) @RequestBody Stock stock) {
        log.info("新增库存：{}", stock);
        stockService.save(stock);
        return Result.success();
    }

    /**
     * 更新库存
     */
    @ApiOperation(value = "更新库存", notes = "更新库存信息")
    @PutMapping
    @OperationLogAnnotation(operation = "更新库存信息", operationType = "更新")
    public Result<String> update(@ApiParam(value = "库存信息", required = true) @RequestBody Stock stock) {
        log.info("更新库存：{}", stock);
        stockService.update(stock);
        return Result.success();
    }

    /**
     * 删除库存
     */
    @ApiOperation(value = "删除库存", notes = "根据库存ID删除库存")
    @DeleteMapping("/{id}")
    @OperationLogAnnotation(operation = "删除库存", operationType = "删除")
    public Result<String> delete(@ApiParam(value = "库存ID", required = true) @PathVariable Long id) {
        log.info("删除库存：{}", id);
        stockService.delete(id);
        return Result.success();
    }

    /**
     * 更新库存数量
     */
    @ApiOperation(value = "更新库存数量", notes = "更新库存数量")
    @PutMapping("/quantity/{id}/{quantity}")
    @OperationLogAnnotation(operation = "更新库存数量", operationType = "更新")
    public Result<String> updateQuantity(
            @ApiParam(value = "库存ID", required = true) @PathVariable Long id,
            @ApiParam(value = "库存数量", required = true) @PathVariable Integer quantity) {
        log.info("更新库存数量：id={}, quantity={}", id, quantity);
        stockService.updateQuantity(id, quantity);
        return Result.success();
    }

    /**
     * 更新锁定库存
     */
    @ApiOperation(value = "更新锁定库存", notes = "更新库存的锁定数量")
    @PutMapping("/locked/{id}/{locked}")
    @OperationLogAnnotation(operation = "更新锁定库存", operationType = "更新")
    public Result<String> updateLocked(
            @ApiParam(value = "库存ID", required = true) @PathVariable Long id,
            @ApiParam(value = "锁定数量", required = true) @PathVariable Integer locked) {
        log.info("更新锁定库存：id={}, locked={}", id, locked);
        stockService.updateLocked(id, locked);
        return Result.success();
    }
}
